﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HomeWork07
{
    public partial class Form1 : Form
    {        
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Pen pen = Pens.Blue;
        public Form1()
        {
            InitializeComponent();
            myColor.SelectedIndex = 3;
        }

        private void draw_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = drawPanel.CreateGraphics();
            graphics.Clear(BackColor);
            drawCayleyTree(depth.Value, drawPanel.Width/2, drawPanel.Height, (double)leng.Value, -Math.PI / 2);
        }


        void drawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(pen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void depth_Click(object sender, EventArgs e)
        {
            depth.Visible = !depth.Visible;
        }


        private void leng_Click(object sender, EventArgs e)
        {
            leng.Visible = !leng.Visible;
        }

        private void per1_Click(object sender, EventArgs e)
        {
            per11.Visible = !per11.Visible;
        }

        private void per2_Click(object sender, EventArgs e)
        {
            per22.Visible = !per22.Visible;
        }

        private void th1_Click(object sender, EventArgs e)
        {
            th11.Visible = !th11.Visible;
        }

        private void th2_Click(object sender, EventArgs e)
        {
            th22.Visible = !th22.Visible;
        }

        private void menu_Click(object sender, EventArgs e)
        {
            panel1.Visible = !panel1.Visible;
        }

        private void per11_Scroll(object sender, EventArgs e)
        {
            per1 = (double)per11.Value/10;
            label3.Text = "右分支长度比:" + per1;
        }

        private void per22_Scroll(object sender, EventArgs e)
        {
            per2 = (double)per22.Value/10;
            label5.Text = "左分支长度比:" + per2;
        }

        private void th11_Scroll(object sender, EventArgs e)
        {
            th1 = (int)per11.Value * Math.PI / 180;
            label4.Text = "右分支角度:" + th11.Value + "°";
        }

        private void th22_Scroll(object sender, EventArgs e)
        {
            th2 = (int)per22.Value * Math.PI / 180;
            label6.Text = "右分支角度:" + th22.Value + "°";
        }

        private void depth_Scroll(object sender, EventArgs e)
        {
            label1.Text = "递归深度:" + depth.Value;
        }

        private void leng_Scroll(object sender, EventArgs e)
        {
            label2.Text = "主干长度:" + leng.Value;
        }

        private void myColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Pen[] pens = { Pens.Black, Pens.Red, Pens.Yellow, Pens.Blue, Pens.Green };
            pen = pens[myColor.SelectedIndex];
        }
    }
}
